pimpas.AJAX.Populate = Class.create( );
pimpas.Extends(pimpas.AJAX.Populate.prototype, pimpas.Object);
pimpas.Extends(pimpas.AJAX.Populate.prototype, pimpas.ListenerSupport.prototype);

pimpas.AJAX.Populate.Listener = Class.create( );
pimpas.Extends(pimpas.AJAX.Populate.Listener.prototype, pimpas.Listener.prototype);
pimpas.Extends(pimpas.AJAX.Populate.prototype, pimpas.AJAX.Populate.Listener.prototype);
pimpas.AJAX.Populate.events = Class.create( );
pimpas.Extends(pimpas.AJAX.Populate.events,
   {
	   unInitialize: "unInitialize",
	   onRequesting: "onRequesting",
	   onRequestComplete: "onRequestComplete",
	   onPopulateStarted: "onPopulateStarted",
	   onPopulateEnded: "onPopulateEnded",
	   onPopulateError: "onPopulateError",
	   onPopulateAbort: "onPopulateAbort"
   }
);

pimpas.Extends(pimpas.AJAX.Populate.prototype, 
   {
		object: null,
		request: null,
		status: null,
		setObject: function(t) {
			this.object = pimpas.DOM.getElement(t);
		},
		getObject: function() {
			return this.object;
		},
		setRequest: function(t) {
			this.request = t;
		},
		getRequest: function() {
			return this.request;
		},
		initialize: function() {
			this.id = pimpas.globalID++;
			this.log = new pimpas.Log();
			this.status = pimpas.AJAX.Populate.events.unInitialize;
			this.listeners = new Array();
		},
		processResponse: function(text, xml) {
			try {
				this.status = this.callListeners(pimpas.AJAX.Populate.events.onPopulateStarted);
				var iterator = xml.eval( new pimpas.XML.Xpath( '//ajax//elements//element' ) );
				var handle = null;
				var handleObject = null;				
				while((el=iterator.iterateNext())!=null) {
					handle = xml.getAsText( xml.eval( new pimpas.XML.Xpath( 'handler' ), el ).iterateNext( ) );
					this.log.log(pimpas.Log.INFO, 'Creating Handler type ['+handle+'] for object '+this.id);
					handleObject = eval("new "+handle+"()");
					handleObject.populate( xml, el );
				}
				
				this.status = this.callListeners(pimpas.AJAX.Populate.events.onPopulateEnded, text, xml);
				this.log.info("[AJAX-POPULATE]: Finished Populate with no errors!");
			} catch(e) {
				this.log.error("[AJAX-POPULATE]: Error executing populate with handler ["+handle+"]");
				this.status = this.callListeners(pimpas.AJAX.Populate.events.onPopulateError, e.message);
				throw e;
			}
		},
		callBack: function(obj, evt, text, xml) {
			this.callListeners(evt, text, xml);
			switch(evt) {
				case pimpas.HTTPRequest.events.onInitialize:
					this.status = this.callListeners(pimpas.AJAX.Populate.events.onRequesting);
				break;
				
				case pimpas.HTTPRequest.events.onComplete:
					this.status = this.callListeners(pimpas.AJAX.Populate.events.onRequestComplete);
					new pimpas.Timer(50, function(){this.processResponse( text, xml )}, this).go();
				break;
				
				case pimpas.HTTPRequest.events.onError:
					this.status = this.callListeners(pimpas.AJAX.Populate.events.onPopulateError, text);
				break;
				
				case pimpas.HTTPRequest.events.onAbort:
					this.status = this.callListeners(pimpas.AJAX.Populate.events.onPopulateAbort);
				break;
			}
		},
		execute: function() {
			if(this.getRequest() == null) {
				this.log.log(pimpas.Log.ERROR, 'cannot execute populate without object request!');
				return;
			}
			this.getRequest().addListener( this );
			if(this.getRequest( ).queueMode)
				HTTPRequestQueue.add(this.getRequest( ));
			else
				this.getRequest().request( );
		}
   }
);

pimpas.AJAX.Populate.Object = Class.create( );
pimpas.Extends(pimpas.AJAX.Populate.Object.prototype, pimpas.Object);
pimpas.Extends(pimpas.AJAX.Populate.Object.prototype,
   {
	   object: null,
	   setObject: function(t) {
	   		this.object = t;  
	   },
	   getObject: function() {
	    	return this.object;  
	   },
	   initialize: function() {
	    	this.id = pimpas.globalID++;
			this.log = new pimpas.Log();
	   },
	   populate: function(xmlObject, xmlElement) {
       		
	   }
   }
);